home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / lha_axeman / lhlist.c < prev    next >
C/C++ Source or Header  |  1995-09-01  |  7KB  |  303 lines

  1. /*----------------------------------------------------------------------*/
  2. /*        LHarc List Command                    */
  3. /*        This is part of LHarc UNIX Archiver Driver        */
  4. /*                                    */
  5. /*        Copyright(C) MCMLXXXIX  Yooichi.Tagawa            */
  6. /*                                    */
  7. /*  V0.00  Original                1988.05.23  Y.Tagawa    */
  8. /*  V1.00  Fixed                1989.09.22  Y.Tagawa    */
  9. /*  V1.01  Bug Fix for month name        1989.12.25  Y.Tagawa    */
  10. /*----------------------------------------------------------------------*/
  11.  
  12. #include "lharc.h"
  13.  
  14. static long packed_size_total;
  15. static long original_size_total;
  16. static int list_files;
  17.  
  18. /*----------------------------------------------------------------------*/
  19. /*                Print Stuff                */
  20. /*----------------------------------------------------------------------*/
  21.  
  22. /* need 14 or 22 (when verbose_listing is TRUE) column spaces */
  23. static void
  24. print_size (packed_size, original_size)
  25.      long packed_size, original_size;
  26. {
  27.   if (verbose_listing)
  28.     printf ("%7d ", packed_size);
  29.  
  30.   printf ("%7d ", original_size);
  31.  
  32.   if (original_size == 0L)
  33.     printf ("******");
  34.   else
  35.     printf ("%3d.%1d%%",
  36.         (int)((packed_size * 100L) / original_size),
  37.         (int)((packed_size * 1000L) / original_size) % 10);
  38. }
  39.  
  40. /* need 12 or 17 (when verbose_listing is TRUE) column spaces */
  41. static void
  42. print_stamp (t)
  43.      time_t t;
  44. {
  45.   static boolean got_now = FALSE;
  46.   static time_t now;
  47.   static unsigned int threshold;
  48.   static char t_month[12*3+1] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  49.   struct tm *p;
  50.  
  51.   if (t == 0)
  52.     {
  53.     printf ("            "); /* 12 spaces */
  54.       return;
  55.     }
  56.  
  57.   if (!got_now)
  58.     {
  59.       now = time ((time_t*)0);
  60.       p = localtime (&now);
  61.       threshold = p->tm_year * 12 + p->tm_mon - 6;
  62.       got_now = TRUE;
  63.     }
  64.  
  65.   p = localtime (&t);
  66.  
  67.   if (p->tm_year * 12 + p->tm_mon > threshold)
  68.     printf ("%.3s %2d %02d:%02d",
  69.         &t_month[p->tm_mon * 3], p->tm_mday, p->tm_hour, p->tm_min);
  70.   else
  71.     printf ("%.3s %2d  %04d",
  72.         &t_month[p->tm_mon * 3], p->tm_mday, p->tm_year + 1900);
  73. }
  74.  
  75. static void
  76. print_bar ()
  77. {
  78.   char *p, *q;
  79.   /* 17+1+(0 or 7+1)+7+1+6+1+(0 or 1+4)+(12 or 17)+1+20 */
  80.   /*       12345678901234567_  1234567_123456  _123456789012   1234 */
  81.  
  82.   if (verbose_listing)
  83.     {
  84.       p = "- ------ ---------- ";
  85.       q = " -------------";
  86.     }
  87.   else
  88.     {
  89.       p = " ";
  90.       q = " --------------------";
  91.     }
  92.  
  93.   if (verbose)
  94.     q = "";
  95.  
  96.   printf ("----------------- ------- ------%s------------%s\n", p, q);
  97. }
  98.  
  99.  
  100. /*----------------------------------------------------------------------*/
  101. /*                                    */
  102. /*----------------------------------------------------------------------*/
  103.  
  104. static void
  105. list_header ()
  106. {
  107.   char *p, *q;
  108.  
  109.   if (verbose_listing)
  110.     {
  111.       p = "PACKED    SIZE  RATIO        CRC   ";
  112.       q = "       NAME";
  113.     }
  114.   else
  115.     {
  116.       p = "  SIZE  RATIO";
  117.       q = "    NAME";
  118.     }
  119.  
  120.   if (verbose)
  121.     q = "";
  122.  
  123.   printf (" PERMSSN  UID GID  %s     STAMP%s\n", p, q);
  124. #if 0
  125.   printf (" PERMSSN  UID GID %s   SIZE  RATIO%s %s    STAMP%s%s\n",
  126.       verbose_listing ? " PACKED " : "",    /* 8,0 */
  127.       verbose_listing ? "  CRC" : "",    /* 5,0 */
  128.       verbose_listing ? "  " : "",        /* 2,0 */
  129.       verbose_listing ? "      " : "   ",        /* 6,3 */
  130.       verbose ? "" : " NAME");
  131. #endif
  132.   print_bar ();
  133. }
  134.  
  135. static void
  136. list_one (hdr)
  137.      register LzHeader *hdr;
  138. {
  139.   register int mode;
  140.   register char *p;
  141.   char method[6];
  142.  
  143.   if (verbose)
  144.     printf ("%s\n", hdr->name);
  145.  
  146.   strncpy(method,hdr->method,5);
  147.   method[5]='\0';
  148.  
  149.   switch ( mode=hdr->extend_type )
  150.   {
  151.       case EXTEND_UNIX:
  152.         mode=hdr->unix_mode;
  153.       printf ("%c%c%c%c%c%c%c%c%c%4d/%-4d",
  154.           ((mode & UNIX_OWNER_READ_PERM)  ? 'r' : '-'),
  155.           ((mode & UNIX_OWNER_WRITE_PERM) ? 'w' : '-'),
  156.           ((mode & UNIX_OWNER_EXEC_PERM)  ? 'x' : '-'),
  157.           ((mode & UNIX_GROUP_READ_PERM)  ? 'r' : '-'),
  158.           ((mode & UNIX_GROUP_WRITE_PERM) ? 'w' : '-'),
  159.           ((mode & UNIX_GROUP_EXEC_PERM)  ? 'x' : '-'),
  160.           ((mode & UNIX_OTHER_READ_PERM)  ? 'r' : '-'),
  161.           ((mode & UNIX_OTHER_WRITE_PERM) ? 'w' : '-'),
  162.           ((mode & UNIX_OTHER_EXEC_PERM)  ? 'x' : '-'),
  163.           hdr->unix_uid, hdr->unix_gid);
  164.       break;
  165.     case EXTEND_OS68K:
  166. /**/    case EXTEND_XOSK: /**/
  167.         mode=hdr->unix_mode;
  168.       printf ("%c%c%c%c%c%c%c%c %4d/%-4d",
  169.           ((mode & OSK_DIRECTORY_PERM)    ?    'd' : '-'),
  170.           ((mode & OSK_SHARED_PERM)     ?     's' : '-'),
  171.           ((mode & OSK_OTHER_EXEC_PERM)    ?     'e' : '-'),
  172.           ((mode & OSK_OTHER_WRITE_PERM)?     'w' : '-'),
  173.           ((mode & OSK_OTHER_READ_PERM)    ?     'r' : '-'),
  174.           ((mode & OSK_OWNER_EXEC_PERM)    ?     'e' : '-'),
  175.           ((mode & OSK_OWNER_WRITE_PERM)?     'w' : '-'),
  176.           ((mode & OSK_OWNER_READ_PERM)    ?     'r' : '-'),
  177.           hdr->unix_uid, hdr->unix_gid);
  178.       break;
  179.     default:
  180.       switch (hdr->extend_type)
  181.         {            /* max 18 characters */
  182.         case EXTEND_GENERIC:p = "[generic]"; break;
  183.         case EXTEND_CPM:    p = "[CP/M]"; break;
  184.         case EXTEND_FLEX:    p = "[FLEX]"; break;
  185.         case EXTEND_OS9:    p = "[OS-9]"; break;
  186.         case EXTEND_OS68K:    p = "[OS-9/68K]"; break;
  187.         case EXTEND_MSDOS:    p = "[MS-DOS]"; break;
  188.         case EXTEND_MACOS:    p = "[Mac OS]"; break;
  189.         case EXTEND_OS2:    p = "[OS/2]"; break;
  190.         case EXTEND_HUMAN:    p = "[Human68K]"; break;
  191.         case EXTEND_OS386:    p = "[OS-386]"; break;
  192.         case EXTEND_RUNSER:    p = "[Runser]"; break;
  193. #ifdef EXTEND_TOWNSOS
  194.           /* This ID isn't fixed */
  195.         case EXTEND_TOWNSOS:    p = "[TownsOS]"; break;
  196. #endif
  197.           /* Ouch!  Please customize it's ID.  */
  198.         default:         p = "[unknown]"; break;
  199.         }
  200.       printf ("%-18.18s", p);
  201.       break;
  202.     }
  203.   
  204.   print_size (hdr->packed_size, hdr->original_size);
  205.  
  206.   if (verbose_listing)
  207.     if (hdr->has_crc)
  208.       printf (" %s %04x", method,hdr->crc);
  209.     else
  210.       printf (" %s ****",method);
  211.   
  212.   printf (" ");
  213.   print_stamp (hdr->unix_last_modified_stamp);
  214.  
  215.   if (!verbose)
  216.     printf (" %s", hdr->name);
  217.  
  218.   printf ("\n");
  219. }
  220.  
  221. static void
  222. list_tailer ()
  223. {
  224.   struct stat stbuf;
  225.  
  226.   print_bar ();
  227.  
  228.   printf (" Total %4d file%c ",
  229.       list_files, (list_files == 1) ? ' ' : 's');
  230.   print_size (packed_size_total, original_size_total);
  231.   printf (" ");
  232.  
  233.   if (verbose_listing)
  234.     printf ("           ");
  235.  
  236.   if (stat (archive_name, &stbuf) < 0)
  237.     print_stamp ((time_t)0);
  238.   else
  239.     print_stamp (stbuf.st_mtime);
  240.  
  241.   printf ("\n");
  242. }
  243.  
  244. /*----------------------------------------------------------------------*/
  245. /*        LIST COMMAND MAIN                    */
  246. /*----------------------------------------------------------------------*/
  247.  
  248. void
  249. cmd_list ()
  250. {
  251.   FILE *afp;
  252.   LzHeader hdr;
  253.   int i;
  254.  
  255.   /* initialize total count */
  256.   packed_size_total = 0L;
  257.   original_size_total = 0L;
  258.   list_files = 0;
  259.  
  260.   /* open archive file */
  261.   if ((afp = open_old_archive ()) == NULL)
  262.     fatal_error (archive_name);
  263.   if (archive_is_msdos_sfx1 (archive_name))
  264.     skip_msdos_sfx1_code (afp);
  265.  
  266.   /* print header message */
  267.   if (!quiet)
  268.     list_header ();
  269.  
  270.   /* print each file information */
  271.   while (get_header (afp, &hdr))
  272.     {
  273.       if (need_file (hdr.name))
  274.     {
  275.       list_one (&hdr);
  276.       list_files ++;
  277.       packed_size_total += hdr.packed_size;
  278.       original_size_total += hdr.original_size;
  279.     }
  280.  
  281.       if (afp != stdin)
  282.       fseek (afp, hdr.packed_size, SEEK_CUR);
  283.       else
  284.     {
  285.       i = hdr.packed_size;
  286.       while(i--) fgetc(afp);
  287.     }
  288.     }
  289.  
  290.   /* close archive file */
  291.   fclose (afp);
  292.  
  293.   /* print tailer message */
  294.   if (!quiet)
  295.     list_tailer ();
  296.  
  297.   return;
  298. }
  299.  
  300.  
  301.  
  302.  
  303.